﻿using MySql.Data.MySqlClient;

namespace Halk.Dao
{
    public abstract class DaoBase
    {
        protected string ConnectionString { get; set; }

        private MySqlConnection _connection;

        protected DaoBase(string connectionString)
        {
            ConnectionString = connectionString;
        }

        protected MySqlConnection Connection
        {
            get
            {
                if (_connection == null)
                {
                    _connection = new MySqlConnection(ConnectionString);
                    _connection.Open();
                }
                return _connection;
            }
        }

        protected void CloseConnection()
        {
            if (Connection != null)
            {
                Connection.Close();
            }
        }

        //protected int ExecuteNonQuery(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
        //{
        //    if (connection == null) throw new ArgumentNullException("connection\r\n");
        //    using (var cmd = new MySqlCommand(commandText,connection))
        //    {
        //        cmd.CommandTimeout = 3800;
        //        foreach (var commandParameter in commandParameters)
        //        {
        //            cmd.Parameters.Add(commandParameter);
        //        }
        //        return  cmd.ExecuteNonQuery(); 
        //    }
        //    return -1;
        //}
    }
}
